package com.leonzx.base.config.init;

import com.leonzx.base.entity.system.User;
import com.leonzx.base.service.system.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @Author:梁亮
 * @Description: 初始化超级管理员
 * @Date: 2018/6/5 9:28
 */
@Component
@Order(1)
public class UserInit implements ApplicationRunner {

    private final Logger logger = LoggerFactory.getLogger(UserInit.class);

    public static final String ADMIN = "admin";
    public static final String ADMIN_NAME = "超级管理员";
    public static final String ADMIN_PASSWORD = "lhzx123";

    @Autowired
    private UserService userService;

    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        User user = userService.getByUsername(ADMIN);
        if (user == null) {
            logger.info("没有发现admin账户，开始创建!");
            userService.createAdmin();
            logger.info("创建账户完成，初始密码为{}", UserInit.ADMIN_PASSWORD);
        }
    }
}
